package com.aizhixin.lab.company.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.UserInforCore;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.LogDomain;
import com.aizhixin.lab.company.domain.QueryLogDomain;
import com.aizhixin.lab.company.domain.QueryUserDomain;
import com.aizhixin.lab.company.service.LogService;
import com.aizhixin.lab.company.service.UserService;
import com.aizhixin.machine.Conversion;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/base/log")
@Api(value = "日志管理相关API")
public class LogController {
	@Autowired
	private LogService logService;
	@Autowired
	private AccountService accountService;

	@RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "保存日志信息", response = Void.class, notes = "保存登录,登出日志信息<br>@author zhengning")
	public ResponseEntity<?> save(
//			@RequestHeader("Authorization") String token,
			@ApiParam(value = "必填字段：userId：操作用户id，operationType:操作类型（登录,登出）", required = false) @RequestBody LogDomain domain) {
//		AccountDTO account = accountService.getUserByToken(token);
//		if (account == null) {
//			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
//					HttpStatus.UNAUTHORIZED);
//		}
		if (null != domain.getUserId() && domain.getUserId() > 0L) {
			Map<String, Object> result = new HashMap<>();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "日志信息缺少操作用户id！");
		}
		if (StringUtils.isEmpty(domain.getOperationType())) {
			Map<String, Object> result = new HashMap<>();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "日志信息缺少操作类型！");
		}

		Map<String, Object> result = logService.save(domain);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	@RequestMapping(value = "/update", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "更新用户操作时间", response = Void.class, notes = "更新用户操作时间<br>@author lwq")
	public ResponseEntity<?> update(
			@RequestHeader("Authorization") String token) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		logService.updateLastTime(account.getId());
		return new ResponseEntity(HttpStatus.OK);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "DELETE", value = "批量删除日志记录", response = Void.class, notes = "批量删除日志记录<br>@author zhengning")
	public ResponseEntity<?> deleteUser(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "批量删除的记录id集合", required = false) @RequestParam(value = "ids", required = false) ArrayList<Long> ids) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = logService.deleteAll(ids);
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/findpage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "日志信息分页查询", response = Void.class, notes = "日志信息分页查询<br>@author zhengning")
	public ResponseEntity<?> findPage(
			@RequestHeader("Authorization") String token,
			@ApiParam(value = "", required = false) @RequestBody QueryLogDomain domain) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(),
					HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = logService.findPage(domain);
		return new ResponseEntity(result, HttpStatus.OK);
	}

}
